<template>
  <el-dialog
    :title="form.id ? '编辑回访' : '新建回访'"
    :visible="value"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-form-item label="回访时间" prop="visitTime">
        <el-date-picker
          v-model="form.visitTime"
          type="date"
          :picker-options="pickerOptions"
          :editable="false"
          :clearable="false"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="跟踪状态" prop="trackStatus">
        <el-select
          v-model="form.trackStatus"
          placeholder="请选择跟踪状态"
          style="width: 220px"
        >
          <el-option
            v-for="option in trackingStatusOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="消费状态" prop="consumeStatus">
        <el-select
          v-model="form.consumeStatus"
          placeholder="请选择消费状态"
          style="width: 220px"
        >
          <el-option
            v-for="option in consumeStatusOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="回访人员" prop="visitPerson">
        <el-select
          v-model="form.visitPerson"
          placeholder="请选择回访人员"
          style="width: 220px"
        >
          <el-option
            v-for="option in tcOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="回访计划" prop="contet">
        <el-input
          v-model="form.contet"
          class="card__input"
          type="textarea"
          :rows="3"
        ></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button type="primary" :loading="formLoading" @click="handleSubmit">
        确定
      </el-button>
      <el-button @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { mapState } from 'vuex'
import {
  consumeStatusOptions,
  returnVisitStatusOptions,
  trackingStatusOptions,
} from '@/utils/options'
import { returnVisitStatusEnum, trackingStatusEnum } from '@/utils/enums'
import { listStaff } from '@/api/services/common'
import { addTc, updateTc } from '@/api/services/tc'
import dayjs from 'dayjs'

const FORM_DATA = {
  visitTime: undefined,
  trackStatus: undefined,
  consumeStatus: undefined,
  visitPerson: undefined,
  visitStatus: undefined,
  contet: undefined,
}

export default {
  name: 'ReturnVisitEditor',
  model: {
    prop: 'value',
    event: 'close',
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    patient: {
      type: Object,
      default: () => ({}),
    },
    data: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      loading: false,
      visible: true,
      tcList: [],
      visitList: [],
      formLoading: false,
      form: {},
      rules: {
        visitPerson: [
          { required: true, message: '回访人员不能为空', trigger: 'blur' },
        ],
        trackStatus: [
          { required: true, message: '跟踪状态不能为空', trigger: 'blur' },
        ],
        consumeStatus: [
          { required: true, message: '消费状态不能为空', trigger: 'blur' },
        ],
        contet: [
          { required: true, message: '回访内容不能为空', trigger: 'blur' },
        ],
        visitTime: [
          { required: true, message: '回访时间不能为空', trigger: 'blur' },
        ],
      },
      pickerOptions: {
        firstDayOfWeek: 1,
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7
        },
      },
    }
  },
  computed: {
    ...mapState({
      userName: (state) => state.user.name,
      clinicId: (state) => state.clinic.clinicId,
      clinicName: (state) => state.clinic.clinicName,
    }),
    trackingStatusOptions() {
      return trackingStatusOptions
    },
    returnVisitStatusOptions() {
      return returnVisitStatusOptions
    },
    consumeStatusOptions() {
      return consumeStatusOptions
    },
    returnVisitStatusEnum() {
      return returnVisitStatusEnum
    },
    trackingStatusEnum() {
      return trackingStatusEnum
    },
    tcOptions() {
      return this.tcList.map(({ userName }) => ({
        label: userName,
        value: userName,
      }))
    },
  },
  watch: {
    value(val) {
      if (val) {
        this.init()
      } else {
        this.$refs.form.resetFields()
      }
    },
  },
  mounted() {
    this.getTcList()
  },
  methods: {
    init() {
      this.form = { ...FORM_DATA, ...this.data }
    },
    handleClose() {
      this.$emit('close', false)
    },
    async getTcList() {
      const { data } = await listStaff({
        deptId: +this.clinicId,
        postname: 'TC',
      })
      this.tcList = data
    },
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        try {
          if (!valid) {
            return
          }
          this.formLoading = true
          if (this.form.id) {
            await updateTc({
              ...this.form,
              visitTime: dayjs(this.form.visitTime).format(
                'YYYY-MM-DD HH:mm:ss'
              ),
            })
            this.msgSuccess('编辑成功')
          } else {
            const {
              id: clinicPersonId,
              caseNumber,
              name,
              phoneNumber,
            } = this.patient
            await addTc({
              ...this.form,
              clinicPersonId,
              caseNumber,
              name,
              phoneNumber,
              clinicId: this.clinicId,
              clinicName: this.clinicName,
              visitStatus: returnVisitStatusEnum['待计划'],
              visitTime: dayjs(this.form.visitTime).format(
                'YYYY-MM-DD HH:mm:ss'
              ),
              createBy: this.userName,
            })
            this.msgSuccess('添加成功')
          }
          this.handleClose()
          this.$emit('ok')
        } finally {
          this.formLoading = false
        }
      })
    },
  },
}
</script>

<style scoped></style>
